package com.saturn.report;

import java.util.List;

import org.json.JSONObject;

import com.saturn.app.db.DymaticCondition;
import com.saturn.app.db.ListData;
import com.saturn.app.db.ORMapping;
import com.saturn.app.db.ResultORMapping;
import com.saturn.app.db.SimpleDaoTemplate;
import com.saturn.app.exception.BSException;

public class ReportView {
	private String id;
	private String name;
	private String title;
	private String dataId;
	private String col;
	private String row;
	private String chart;
	private String path;

	private static ORMapping<ReportView> mapping = new ResultORMapping<ReportView>();

	public static int add(ReportView view) {
		if (get(view.getId()) != null) {
			throw new BSException("报表数据源ID重复,id=" + view.getId());
		}

		return SimpleDaoTemplate
				.update("INSERT INTO report_view VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
						view.getId(), view.getName(), view.getTitle(),
						view.getDataId(), view.getCol(), view.getRow(),
						view.getChart(),view.getPath());

	}

	public static int edit(ReportView view) {
		return SimpleDaoTemplate
				.update("UPDATE report_view SET name = ?, title = ?, dataId = ?, col = ?, row = ?, chart = ?, path = ? WHERE id = ?",
						view.getName(), view.getTitle(), view.getDataId(),
						view.getCol(), view.getRow(), view.getChart(),view.getPath(),
						view.getId());
	}

	public static ReportView get(String id) {
		return SimpleDaoTemplate.queryOne(
				"SELECT * FROM report_view WHERE id = '" + id + "'", null,
				mapping, ReportView.class);
	}
	
	public static ListData<ReportView> getReportViews(ReportView reportView, String start,
			String offset, String orderBy, String order) {

		DymaticCondition addCondition = new DymaticCondition()
				.addSimpleCondition(reportView, "id", "name", "title", "dataId", "col", "row", "chart", "path");

		int total = SimpleDaoTemplate.queryCount(
				"SELECT count(*) FROM report_view WHERE 1 = 1", addCondition);

		List<ReportView> list = SimpleDaoTemplate.query(
				"SELECT * FROM report_view WHERE 1 = 1", addCondition
						.addCondition("ORDER BY {0} {1}", orderBy, order)
						.addCondition("LIMIT {0}, {1}", start, offset),
				mapping, ReportView.class);

		return new ListData<ReportView>(total, list);
	}

	public static ListData<ReportView> getReportDatas(ReportView user,
			String start, String offset, String orderBy, String order) {

		DymaticCondition addCondition = new DymaticCondition()
				.addSimpleCondition(user, "id", "name", "title", "dataId");

		int total = SimpleDaoTemplate.queryCount(
				"SELECT count(*) FROM report_view WHERE 1 = 1", addCondition);

		List<ReportView> list = SimpleDaoTemplate.query(
				"SELECT * FROM report_view WHERE 1 = 1", addCondition
						.addCondition("ORDER BY {0} {1}", orderBy, order)
						.addCondition("LIMIT {0}, {1}", start, offset),
				mapping, ReportView.class);

		return new ListData<ReportView>(total, list);
	}

	public static int remove(String id) {
		return SimpleDaoTemplate.update("DELETE FROM report_view WHERE id = ?",
				id);
	}

	public static void removes(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				remove(id);
			}
		}
	}

	public ReportView() {
	}

	public ReportView(String id, String name, String title, String dataId,
			String col,String row,String chart,String path) {
		this.id = id;
		this.name = name;
		this.title = title;
		this.dataId = dataId;
		this.col = col;
		this.row = row;
		this.chart = chart;
		this.path = path;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDataId() {
		return dataId;
	}

	public void setDataId(String dataId) {
		this.dataId = dataId;
	}

	public String getCol() {
		return col;
	}

	public void setCol(String col) {
		this.col = col;
	}

	public String getRow() {
		return row;
	}

	public void setRow(String row) {
		this.row = row;
	}

	public String getChart() {
		return chart;
	}

	public void setChart(String chart) {
		this.chart = chart;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@Override
	public String toString() {
		return new JSONObject(this).toString();
	}

}
